package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.SoulPaper;
import com.tanhua.domain.mongo.SoulQuestion;
import com.tanhua.domain.mongo.SoulQuestionOption;
import com.tanhua.domain.mongo.SoulResult;
import com.tanhua.domain.vo.OptionVO;
import com.tanhua.domain.vo.SoulPaperVO;
import com.tanhua.domain.vo.SoulQuestionVO;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class SoulPaperApiImpl implements SoulPaperApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<SoulPaperVO> findPaperList(Long userId) {
        //查询所有问卷
        List<SoulPaper> soulPapers = mongoTemplate.find(new Query(), SoulPaper.class);
        //转换vo
        List<SoulPaperVO> papers = new ArrayList<>();

        //用来判断当前测试问卷是否有测试报告
        boolean lastHasResult = false;

        for (SoulPaper soulPaper : soulPapers) {
            SoulPaperVO paper = new SoulPaperVO();
            //查找问卷中问题的集合
            List<SoulQuestionVO> questions = findQuestions(soulPaper);
            //封装问卷
            paper.setQuestions(questions);
            paper.setName(soulPaper.getName());
            paper.setLevel(soulPaper.getLevel());
            paper.setId(soulPaper.getId().toHexString());
            paper.setCover(soulPaper.getCover());
            paper.setStar(soulPaper.getStar());

            //根据用户id和问卷id确定当前问卷是否已有测试结果
            Query query = new Query();
            query.addCriteria(Criteria.where("userId").is(userId)
                    .and("paperId").is(soulPaper.getId()));

            SoulResult soulResult = mongoTemplate.findOne(query, SoulResult.class);

            //根据有无测试结果确定问卷锁定状态，问卷报告
            // soulResult判断的是当前问卷是否有测试结果，lastHasResult是判断上一级问卷是否有测试结果
            if (soulResult == null) {
                if (lastHasResult) {
                    //如果上一级有测试结果，设置当前测试解锁状态
                    paper.setIsLock(0);
                } else {
                    paper.setIsLock(1);
                }
                //如果是初级问卷解锁
                if ("初级".equals(paper.getLevel())) {
                    paper.setIsLock(0);
                }
                lastHasResult = false;
                paper.setReportId(null);
                //如果查询不到前一级的结果就锁定
            } else {
                lastHasResult = true;
                //如果当前paper存在结果，设置下一级paper为解锁状态
                paper.setIsLock(0);
                //根据soulResult中的score 决定报告id
                paper.setReportId(soulResult.getReportId().toHexString());
            }
            papers.add(paper);
        }
        return papers;
    }


    //根据问卷查找问题
    private List<SoulQuestionVO> findQuestions(SoulPaper soulPaper) {
        List<SoulQuestion> soulQuestions = mongoTemplate.find(Query.query(Criteria.where("soulPaperId").is(soulPaper.getId())), SoulQuestion.class);
        List<SoulQuestionVO> questions = new ArrayList<>();

        for (SoulQuestion soulQuestion : soulQuestions) {
            SoulQuestionVO question = new SoulQuestionVO();
            //查询每个问题中的选项
            List<OptionVO> options = findOptions(soulQuestion);
            //封装问题
            question.setOptions(options);
            question.setQuestion(soulQuestion.getQuestion());
            question.setId(soulQuestion.getId().toHexString());
            questions.add(question);
        }
        return questions;
    }

    //根据问题查找选项
    private List<OptionVO> findOptions(SoulQuestion soulQuestion) {
        List<SoulQuestionOption> soulOptions = mongoTemplate.find(Query.query(Criteria.where("questionId").is(soulQuestion.getId())), SoulQuestionOption.class);
        List<OptionVO> options = new ArrayList<>();

        for (SoulQuestionOption soulOption : soulOptions) {
            //封装选项
            OptionVO option = new OptionVO();
            option.setId(soulOption.getId().toHexString());
            option.setOption(soulOption.getOption());
            options.add(option);
        }
        return options;
    }
}
